package com.woniuxy.credit_loans.staff_dong.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.credit_loans.entity.Emp;
import com.woniuxy.credit_loans.staff_dong.dto.EmpDto;
import com.woniuxy.credit_loans.staff_dong.service.EmpService;
import com.woniuxy.credit_loans.staff_dong.service.PermissionService;
import com.woniuxy.credit_loans.staff_dong.vo.EmpVo;
import com.woniuxy.credit_loans.staff_dong.vo.PermissionVo;
import com.woniuxy.credit_loans.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    @Autowired
    private PermissionService permissionService;

    @PostMapping("login")
    private Result emplogin(@RequestBody EmpDto empDto){
            Emp login = empService.login(empDto);
            if (login != null) {
                List<PermissionVo> permissionVos = permissionService.selMenuByAcc(empDto.getEmpAcc(), 0);
                return Result.ok("登录成功").data("menuList",permissionVos).data("empLi",login);

            } else {
                return Result.error("账户信息异常，请联系主管解决");
            }
    }

    @PostMapping("/selAll")
    private Result selAll(){
        List<Emp> emps = empService.selAll();
        return Result.ok().data("empList",emps);
    }

    //查询所有产品经理的姓名 刘杰 2022/12/9 14:24
    @GetMapping("/productLeader")
    public Result selAllProductLeaders(){
        List<Emp> leaders = empService.selAllProductLeaders();
        return  Result.ok().data("leader",leaders);
    }

    @PostMapping("/query")
    public Result qurry(@RequestBody EmpDto empDto){
        PageHelper.startPage(empDto.getCurrentPage(),empDto.getPageSize());
        List<EmpVo> empVos = empService.selectByLike(empDto);
        PageInfo<EmpVo> empVoPageInfo = new PageInfo<>(empVos);
        return Result.ok().data("empList",empVos).data("total",empVoPageInfo.getTotal());
    }

    @PostMapping("")
    public Result add(@RequestBody Emp emp){
        int i = empService.insertSelective(emp);
        return Result.ok();
    }

    @PutMapping("")
    public Result update(@RequestBody Emp emp){
        int i = empService.updateByPrimaryKeySelective(emp);
        return Result.ok();
    }

    @DeleteMapping ("{empId}")
    public Result del(@PathVariable Long empId){
        int i = empService.deleteByPrimaryKey(empId);
        return Result.ok();
    }

    @GetMapping("{empId}")
    public Result selOne(@PathVariable Long empId){
        Emp emp = empService.selectByPrimaryKey(empId);
        return Result.ok().data("emp",emp);
    }
}
